<template>
  <div class="app-container">
    <!-- 查询条件框 -->
    <el-card v-show="!isReview" class="box-card">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="文章名称">
          <el-input v-model="form.title" placeholder="请输入文章名称" clearable />
        </el-form-item>
        <el-form-item label="文章类别">
          <el-select v-model="form.type" placeholder="请选择文章类别">
            <el-option v-for="item in blogTypes" :key="item.id" :label="item.typeName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <el-divider />
      <!-- 文章信息列表 -->
      <el-table :data="blogs" style="width: 100%" stripe class="table">
        <el-table-column prop="bid" label="编号" width="100" />
        <el-table-column prop="cover" label="封面" width="100">
          <template slot-scope="scope">
            <el-image style="width: 80px; height: 50px" :src="scope.row.cover" :preview-src-list="[scope.row.cover]" lazy fit>
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="文章标题" class="table-title" :show-overflow-tooltip="true" />
        <el-table-column prop="username" label="作者">
          <!-- <template slot-scope="scope">
            {{ scope.row.user.username }}
          </template> -->
        </el-table-column>
        <el-table-column prop="createTime" label="发布时间">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="文章类别">
          <template slot-scope="scope">
            {{ getTypeName(scope.row.type, blogTypes) }}
          </template>
        </el-table-column>
        <el-table-column prop="viewCount" label="浏览量">
          <!-- <template>
            <el-row>
              <el-button type="primary" icon="el-icon-more-outline" circle />
            </el-row>
          </template> -->
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag v-show="scope.row.status===2" type="success">已审核</el-tag>
            <!-- <el-tag v-show="scope.row.status===2" type="success" @click="goDetail(scope.row.bid)">已审核</el-tag> -->
            <el-tag v-show="scope.row.status===1" type="danger">不通过</el-tag>
            <!-- <el-tag v-show="!scope.row.status" type="warning" @click="addBlogReview(scope.row.bid)">待审核</el-tag> -->
            <el-tag v-show="!scope.row.status" type="warning" @click="goDetail(scope.row.bid)">待审核</el-tag>
            <!-- {{ status[scope.row.status].name }} -->
          </template>
        </el-table-column>
        <!-- <el-table-column prop="viewCount" label="浏览量">
          <template>
            <el-row>
              <el-button type="primary" icon="el-icon-more-outline" circle />
            </el-row>
          </template>
        </el-table-column> -->
      </el-table>
      <el-pagination :page-size="form.pageSize" :current-page="form.pageNum" :pager-count="11" layout="prev, pager, next" :total="total" class="bottom" @current-change="handleCurrentChange" />
    </el-card>
    <el-dialog
      title="审核"
      :visible.sync="reviewDialogVisible"
      width="40%"
      center
      destroy-on-close
    >
      <el-form :model="reviewForm">
        <el-form-item label="审核意见">
          <el-select v-model="reviewForm.status" placeholder="请选择状态">
            <el-option label="审核不通过" value="1">审核不通过</el-option>
            <el-option label="审核通过" value="2">审核通过</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="意见回复">
          <el-input v-model="reviewForm.content" type="textarea" placeholder="请输入意见回复" :rows="2" />
        </el-form-item>
        <!-- <el-form-item>
            <el-button type="primary" @click="addReview">确定</el-button>
            <el-button @click="reviewDialogVisible=false">取消</el-button>
          </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addReview">确定</el-button>
        <el-button @click="reviewDialogVisible=false">取消</el-button>
      </div>
    </el-dialog>
    <!-- <BlogReview :review-dialog-visible.sync="reviewDialogVisible" /> -->
    <BlogDetail v-if="isReview" :bid="blogId" :is-review.sync="isReview" />
  </div>
</template>

<script>
import { getAllBlogs, searchBlogList, addBlogReview } from '@/api/blog'
import { getTypeName, parseTime } from '@/utils/blogUtil'
import { mapGetters } from 'vuex'
import BlogDetail from './BlogDetail.vue'

export default {
  name: 'Blog',
  inject: ['reload'],
  components: {
    BlogDetail
  },
  data() {
    return {
      pageSize: 5,
      pageNum: 1,
      form: {
        pageSize: 5,
        pageNum: 1,
        title: '',
        type: null
      },
      blogs: [],
      pageSum: 0,
      total: 0,
      getTypeName,
      parseTime,
      status: [
        { id: 0, name: '未审核' },
        { id: 1, name: '审核不通过' },
        { id: 2, name: '已审核' }
      ],
      reviewDialogVisible: false,
      reviewForm: {
        status: null,
        content: ''
      },
      isReview: false,
      blogId: 0
    }
  },
  computed: {
    ...mapGetters([
      'blogTypes'
    ])
  },
  watch: {
    isReview: {
      handler(val) {
        this.search()
      }
    }
  },
  created() {
    this.getBlogTypes()
    this.getBlogList()
    // this.getBlogs()
  },
  methods: {
    async getBlogs() {
      const res = await getAllBlogs(this.pageNum, this.pageSize)
      if (res.code === 200) {
        const { blogs, total } = res.data
        this.blogs = blogs
        this.total = total
      } else {
        this.$message.error(res.message)
      }
    },
    getBlogTypes() {
      this.$store.dispatch('blog/getBlogTypes')
      console.log('blogtypes: ' + this.blogTypes)
    },
    async getBlogList() {
      console.log(this.form)
      const res = await searchBlogList(this.form)
      if (res.code === 200) {
        const { blogList, total } = res.data
        this.blogs = blogList
        this.total = total
      }
    },
    // 查询
    search() {
      this.getBlogList().then(res => {
        if (res.code === 200) {
          this.$message.success('查询成功')
        } else {
          this.$message.error('查询失败')
        }
      })
    },
    addBlogReview(id) {
      // 数据
      console.log('data: {}', id)
      this.reviewForm.blogId = id
      // 显示提示框
      this.reviewDialogVisible = true
    },
    async addReview() {
      console.log(this.reviewForm)
      const res = await addBlogReview(this.reviewForm)
      if (res.code === 200) {
        this.$message.success('操作成功')
        this.search()
        this.reviewDialogVisible = false
      } else {
        this.$message.error('操作失败')
      }
    },
    handleCurrentChange(val) {
      this.form.pageNum = val
      this.getBlogList(this.form)
      // .then(res => {
      //   this.form.pageNum = 1
      // })
    },
    goDetail(id) {
      console.log(id)
      this.blogId = id
      this.isReview = true
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  width: 100%;

  .box-card {
    width: 100%;
    height: 100%;

    .table {
      width: 100%;
      height: 470px;

      .table-title {
        word-wrap: break-word;
        text-overflow: ellipsis;
      }
    }

    .bottom {
      margin-top: 10px;
      display: flex;
      justify-content: center;
    }

  }
}
</style>
